﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using 新门禁.Utils;

namespace 新门禁
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
            this.Loaded += Win_loaded;
        }

        private void Button_Click_Login(object sender, RoutedEventArgs e)
        {
            //TcpRequestUtil.Main("out");

            string userName = txtUserName.Text;
            string password = txtPassword.Password;

            if ("".Equals(userName))
            {
                MessageBox.Show("请输入用户名");
                return;
            }
            if ("".Equals(password))
            {
                MessageBox.Show("请输入密码");
                return;
            }

            JObject json = new JObject();
            json.Add("account", userName);
            json.Add("password", password);
            //发送http请求
            string response = HttpPostHelper.doHttpPost("http://" + App.GetBaseInfo().HttpServerIp + ":" + App.GetBaseInfo().AuthHttpServerPort + "/guns/loginApi", json.ToString());
            JObject respJson = JObject.Parse(response);
            string status = respJson["success"].ToString();
            if (status.ToLower() != "true")
            {
                MessageBox.Show("登录失败");
                return;
            }

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if ((bool)cbSavePassword.IsChecked)
            {
                String saveUsernameAndPassword = null;
                if (config.AppSettings.Settings["saveUsernameAndPassword"] != null)
                {
                    saveUsernameAndPassword = config.AppSettings.Settings["saveUsernameAndPassword"].Value;
                }
                if (saveUsernameAndPassword == null)
                {
                    config.AppSettings.Settings.Add("saveUsernameAndPassword", "1");
                    config.AppSettings.Settings.Add("savingUsername", userName);
                    config.AppSettings.Settings.Add("savingPassword", password);
                }
                else
                {
                    config.AppSettings.Settings["savingUsername"].Value = userName;
                    config.AppSettings.Settings["savingPassword"].Value = password;
                }
            }
            else
            {
                config.AppSettings.Settings.Remove("saveUsernameAndPassword");
                config.AppSettings.Settings.Remove("savingUsername");
                config.AppSettings.Settings.Remove("savingPassword");
            }
            config.Save(ConfigurationSaveMode.Modified);
            //JObject date = new JObject();
            JObject data = (JObject)respJson["data"];
            //App.GetGlobalVariablesModel().LoginCorpID = data["companyCode"].ToString();
            //App.GetGlobalVariablesModel().LoginUserID = data["userId"].ToString();
            //App.GetGlobalVariablesModel().LoginUserCode = data["userCode"].ToString();
            //App.GetGlobalVariablesModel().LoginUserName = data["name"].ToString();
            //App.GetGlobalVariablesModel().LoginUserDeptCode = data["deptCode"].ToString();
            //App.GetGlobalVariablesModel().LoginUserDeptName = data["deptName"].ToString();
            App.GetGlobalVariablesModel().LoginUserName = data["loginUser"]["simpleUserInfo"]["realName"].ToString();
            //App.GetGlobalVariablesModel().DIDOControl = data["loginUser"]["buttonCodes"];
            App.GetGlobalVariablesModel().Authorization = data["token"].ToString();
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            this.Close();
        }

        private void Win_loaded(object sender, RoutedEventArgs e)
        {
            String saveUserNameAndPassword = ConfigurationManager.AppSettings["saveUserNameAndPassword"];
            if (null != saveUserNameAndPassword)
            {
                txtUserName.Text = ConfigurationManager.AppSettings["savingUserName"];
                txtPassword.Password = ConfigurationManager.AppSettings["savingPassword"];
                cbSavePassword.IsChecked = true;
            }
            else
            {
                cbSavePassword.IsChecked = false;
            }
        }
    }
}